package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.CarTaskExecutionInfoHisServiceBiz;
import com.mt.roll.core.dal.service.CarTaskExecutionInfoServiceBiz;
import com.mt.roll.core.dal.service.StorageBoxCircInfoHisServiceBiz;
import com.mt.roll.core.dal.service.StorageBoxCircInfoServiceBiz;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.CarTaskExecutionInfoVoMapper;
import com.mt.roll.wms.params.CallBackParam;
import com.mt.roll.wms.params.CarTaskExecutionInfoQueryParam;
import com.mt.roll.wms.service.CarTaskExecutionInfoService;
import com.mt.roll.wms.vo.CarTaskExecutionInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther why
 * @date 2021/9/27 15:55
 */
@Service
@Transactional
@Slf4j
public class CarTaskExecutionInfoServiceImpl extends BaseService implements CarTaskExecutionInfoService {

    @Autowired
    private CarTaskExecutionInfoServiceBiz carTaskExecutionInfoServiceBiz;

    @Autowired
    private CarTaskExecutionInfoVoMapper carTaskExecutionInfoVoMapper;

    @Autowired
    private CarTaskExecutionInfoHisServiceBiz carTaskExecutionInfoHisServiceBiz;

    @Autowired
    private StorageBoxCircInfoServiceBiz storageBoxCircInfoServiceBiz;

    @Autowired
    private StorageBoxCircInfoHisServiceBiz storageBoxCircInfoHisServiceBiz;

    @Override
    public R<PageVo<CarTaskExecutionInfoVo>> page(CarTaskExecutionInfoQueryParam carTaskExecutionInfoQueryParam) {
        IPage<CarTaskExecutionInfoVo> page = carTaskExecutionInfoVoMapper.page(new Page<>(carTaskExecutionInfoQueryParam.getCurrent(),carTaskExecutionInfoQueryParam.getSize()), carTaskExecutionInfoQueryParam);
        return successful(new PageVo<>(page,CarTaskExecutionInfoVo.class));
    }

    @Override
    public R<CarTaskExecutionInfoVo> get(IdParam idParam) {
        CarTaskExecutionInfo carTaskExecutionInfo = carTaskExecutionInfoServiceBiz.getById(idParam.getId());
        Assert.notNull("任务详情不存在", carTaskExecutionInfo);
        CarTaskExecutionInfoVo carTaskExecutionInfoVo = carTaskExecutionInfoVoMapper.getById(idParam.getId());
        return successful(carTaskExecutionInfoVo);
    }

    @Override
    public R<IdVo> rgvTaskEnd(CallBackParam callBackParam) {
        //通过任务id找到对应的任务信息
        CarTaskExecutionInfo carTaskExecutionInfo = carTaskExecutionInfoServiceBiz.getById(callBackParam.getTaskId());
        //把任务的信息存到历史表
        CarTaskExecutionInfoHis carTaskExecutionInfoHis = new CarTaskExecutionInfoHis();
        BeanUtils.copyProperties(carTaskExecutionInfo, carTaskExecutionInfoHis);
        carTaskExecutionInfoHis.setId(null);
        carTaskExecutionInfoHisServiceBiz.save(carTaskExecutionInfoHis);
        //获得两个当前库位的信息
        QueryWrapper<StorageBoxCircInfo> emptyStorageBoxCircInfoQueryWrapper = new QueryWrapper<>();
        emptyStorageBoxCircInfoQueryWrapper.eq(ObjectUtils.isNotNull(carTaskExecutionInfo.getCurrLocationId()), CarTaskExecutionInfo.CURR_LOCATION_ID, carTaskExecutionInfo.getCurrLocationId());
        //空的库位存储信息
        StorageBoxCircInfo emptyStorageBoxCircInfo = storageBoxCircInfoServiceBiz.getOne(emptyStorageBoxCircInfoQueryWrapper);

        QueryWrapper<StorageBoxCircInfo> fullStorageBoxCircInfoQueryWrapper = new QueryWrapper<>();
        fullStorageBoxCircInfoQueryWrapper.eq(ObjectUtils.isNotNull(carTaskExecutionInfo.getTargetLocationId()), CarTaskExecutionInfo.TARGET_LOCATION_ID, carTaskExecutionInfo.getCurrLocationId());
        //有货物的库位存储信息
        StorageBoxCircInfo fullStorageBoxCircInfo = storageBoxCircInfoServiceBiz.getOne(fullStorageBoxCircInfoQueryWrapper);

        //把库位信息修改
        //把各个需要更新的字段需要先拿出来，然后一个一个设置
        //空的存储箱信息
        Long emptyStorageBoxId = emptyStorageBoxCircInfo.getStorageBoxId();
        String emptyStorageCode = emptyStorageBoxCircInfo.getStorageCode();
        //有货存储箱信息
        Long fullStorageBoxId = fullStorageBoxCircInfo.getStorageBoxId();
        String fullStorageCode = fullStorageBoxCircInfo.getStorageCode();
        Long locationStorageSubId = fullStorageBoxCircInfo.getLocationStorageSubId();
        //设置空的存储箱信息
        emptyStorageBoxCircInfo.setStorageBoxId(fullStorageBoxId);
        emptyStorageBoxCircInfo.setStorageCode(fullStorageCode);
        emptyStorageBoxCircInfo.setLocationStorageSubId(locationStorageSubId);
        emptyStorageBoxCircInfo.setIsEmpty(1);

        storageBoxCircInfoServiceBiz.update(emptyStorageBoxCircInfo,new QueryWrapper<>());

        StorageBoxCircInfoHis emptyStorageBoxCircInfoHis = new StorageBoxCircInfoHis();
        BeanUtils.copyProperties(emptyStorageBoxCircInfo, emptyStorageBoxCircInfoHis);
        emptyStorageBoxCircInfoHis.setId(null);
        storageBoxCircInfoHisServiceBiz.save(emptyStorageBoxCircInfoHis);

        //设置有货存储箱信息
        fullStorageBoxCircInfo.setStorageBoxId(emptyStorageBoxId);
        fullStorageBoxCircInfo.setStorageCode(emptyStorageCode);
        fullStorageBoxCircInfo.setLocationStorageSubId(null);
        fullStorageBoxCircInfo.setIsEmpty(0);
        storageBoxCircInfoServiceBiz.update(fullStorageBoxCircInfo,new QueryWrapper<>());

        StorageBoxCircInfoHis fullStorageBoxCircInfoHis = new StorageBoxCircInfoHis();
        BeanUtils.copyProperties(fullStorageBoxCircInfo, fullStorageBoxCircInfoHis);
        fullStorageBoxCircInfoHis.setId(null);
        storageBoxCircInfoHisServiceBiz.save(fullStorageBoxCircInfoHis);

        //移除已经完成的任务
        carTaskExecutionInfoServiceBiz.removeById(callBackParam.getTaskId());
        return successful(IdVo.builder().id(fullStorageBoxCircInfoHis.getId()).build());
    }

    @Override
    public R<IdVo> mesTask(CarTaskExecutionInfo carTaskExecutionInfo) {
        setCommonField(carTaskExecutionInfo);
        carTaskExecutionInfoServiceBiz.save(carTaskExecutionInfo);
        return successful(IdVo.builder().id(carTaskExecutionInfo.getId()).build());
    }
}
